#-*-coding:utf-8-*-
import sys
import re
import collections
import os

def img_list(dir):
    return [x for x in os.listdir(dir) if os.path.isfile(x) and os.path.splitext(x)[1]=='.txt' ]

def word_number(path,name):
    with open(path,'r',encoding = 'utf-8') as f:
        txt = f.read()
        word_list = re.split(r'[\s\,\.\!\*]+',txt)
    print ("words number:",len(word_list))
    relusts = collections.Counter(word_list)
    print("文件 %s " %name)
    #------------使用most_common()排序------------#
    for i,relust in enumerate(relusts.most_common()):
        print (relust)
        if i == 9:
            break

    #--------------使用sorted排序----------------#		
    dict = sorted(relusts.items(),key = lambda d:d[1],reverse = True)
    for i,relust in enumerate(dict):
#	print(relust)
        if i==99:
            break

if __name__ == '__main__':
    start_path = '.'
    txt_lists = img_list(start_path)
    txt_paths = []
    for txt in txt_lists:
        txt_paths.append(os.path.join(start_path,txt))

    for i,path in enumerate(txt_paths):
        word_number(path,txt_lists[i])
